package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.*;
import com.mars.module.admin.mapper.NewsMapper;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.UserBrowseHistoryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.UserBrowseHistoryMapper;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IUserBrowseHistoryService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户浏览历史业务层处理
 *
 * @author mars
 * @date 2024-03-04
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserBrowseHistoryServiceImpl implements IUserBrowseHistoryService {

    private final UserBrowseHistoryMapper baseMapper;
    private final NewsMapper newsMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserBrowseHistory add(UserBrowseHistoryRequest request) {
        UserBrowseHistory entity = UserBrowseHistory.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setUserId(userId);
        entity.setCreateById(userId);
        Long count = baseMapper.selectCount(new QueryWrapper<UserBrowseHistory>().eq("user_id", userId).eq("news_id", request.getNewsId()));
        if (count == 0L) {
            baseMapper.insert(entity);
        }
        News news = newsMapper.selectById(request.getNewsId());
        news.setViews(news.getViews() == null ? 0 : news.getViews() + 1);
        newsMapper.updateById(news);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(String newsId, String userId) {
        baseMapper.delete(new QueryWrapper<UserBrowseHistory>().eq("news_id", newsId).eq("user_id", userId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(UserBrowseHistoryRequest request) {
        UserBrowseHistory entity = UserBrowseHistory.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public UserBrowseHistory getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<UserBrowseHistory> pageList(UserBrowseHistoryRequest request) {
        Page<UserBrowseHistory> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<UserBrowseHistory> query = this.buildWrapper(request);
        IPage<UserBrowseHistory> pageRecord = baseMapper.selectPage(page, query);
        List<UserBrowseHistory> records = pageRecord.getRecords();
        List<UserBrowseHistory> recordsNew = new ArrayList<>();
        for (UserBrowseHistory history : records) {
            News news = newsMapper.selectById(history.getNewsId());
            history.setNewTitle(news == null ? "" : news.getTitle());
            recordsNew.add(history);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<UserBrowseHistory> list(UserBrowseHistoryRequest request) {
        LambdaQueryWrapper<UserBrowseHistory> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<UserBrowseHistory> buildWrapper(UserBrowseHistoryRequest param) {
        LambdaQueryWrapper<UserBrowseHistory> query = new LambdaQueryWrapper<>();
        return query;
    }

}
